﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Se7en.Admin.Business;
using Se7en.Admin.Model;

namespace Se7en.Admin.Common
{
    public class AdminPageBase : PageBase
    {
        #region Properties

        public User UserLogin
        {
            get
            {
                return Session[Keys.Session.UserLogin] as User;
            }
            set
            {
                Session[Keys.Session.UserLogin] = value;
            }
        }

        public List<Menu> Menus
        {
            get
            {
                return Session[Keys.Session.UserMenu] as List<Menu>;
            }
            set
            {
                Session[Keys.Session.UserMenu] = value;
            }
        }

        protected override string LoginUrl
        {
            get
            {
                return PageConstants.AdminUrl.Root + PageConstants.AdminUrl.Login;
            }
        }

        protected override string BadRequestUrl
        {
            get { return PageConstants.AdminUrl.Root + PageConstants.AdminUrl.BadRequest; }
        }


        #endregion

        #region Methods

        protected override bool Authenticate()
        {
            //return true;
            return (this.Menus != null && this.Menus.Select(m => m.Url).Contains(this.AppRelativeVirtualPath));
        }

        public bool Login(string loginName, string password, bool isAdmin)
        {
            User user = DBContext.User_Login(loginName, password, isAdmin);
            if (user != null)
            {
                Session[Keys.Session.UserLogin] = user;
                this.Menus = DBContext.Menu_GetByUserID(user.UserID);
                return true;
            }
            return false;
        }

        #endregion
    }
}
